import 'package:flutter/material.dart';

//Flutter 富文本
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController? tabController;

  //页面创建
  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    //获取
    final TabController newController = DefaultTabController.of(context);
    if (tabController != null) {
      if (newController != tabController) {
        tabController!.removeListener(_handleTabListener);
        newController.addListener(_handleTabListener);
        tabController = newController;
      }
    } else {
      newController.addListener(_handleTabListener);
      tabController = newController;
    }
  }

  void _handleTabListener() {
    print('listener debug');
    //是否正在滑动
    bool scroll = tabController!.indexIsChanging;
    //
    int index = tabController!.index;

    if (scroll) {
      print('正在滑动 $index');
    }

    if (!scroll) {
      print('滑动结束 $index');
      //
    }
  }

  //页面销毁
  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //脚手架
    return DefaultTabController(
      length: 8,
      initialIndex: 4,
      child: Scaffold(
        appBar: AppBar(
          title: Text('标题'),
          backgroundColor: Colors.blue,
          foregroundColor: Colors.white,
          bottom: TabBar(
            //是否可以滚动
            isScrollable: true,
            //指示器的颜色
            indicatorColor: Colors.yellow,
            //厚度
            indicatorWeight: 5,
            //内边距
            indicatorPadding: EdgeInsets.only(top: 0, bottom: 2),

            //自定义选中的装饰
            indicator: UnderlineTabIndicator(
              borderSide: BorderSide(color: Colors.red, width: 4),
              borderRadius: BorderRadius.circular(30),
            ),

            padding: EdgeInsets.only(left: 0, right: 0),
            tabAlignment: TabAlignment.center,
            indicatorSize: TabBarIndicatorSize.label,

            dividerHeight: 2,
            dividerColor: Colors.black,

            //选中的文字颜色
            labelColor: Colors.red,
            //所有的文字颜色  优先选中的
            labelStyle: TextStyle(fontSize: 18),
            labelPadding: EdgeInsets.only(left: 20, right: 20),

            //未选中 标签 的文本的颜色
            unselectedLabelColor: Colors.black87,
            //未选中 标签 的文本的样式
            unselectedLabelStyle: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16,
            ),

            tabs: [
              Tab(text: "标签1"),
              Tab(text: "标签2"),
              Tab(text: "标签3标签3"),
              Tab(text: "标签4"),
              Tab(text: "标签5"),
              Tab(text: "标签6"),
              Tab(text: "标签7"),
              Tab(text: "标签8"),
            ],
          ),
        ),
        body: TabBarView(
          controller: tabController,
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本1'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本3'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本4'),
            ),

            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本5'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本6'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本7'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.orange,
              child: Text('文本8'),
            ),
          ],
        ),
        // This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}
